package com.example.soss.service;

import com.example.soss.entity.Order;
import com.example.soss.entity.Result;
import com.example.soss.entity.ResultEnum;
import com.example.soss.mapper.CourseMapper;
import com.example.soss.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderService {


    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CourseMapper courseMapper;


    /**
     * 下单
     *
     * @param courseId
     * @param stuId
     * @return
     */
    @Transactional
    public Result order(int courseId, int stuId) {
        //该课程剩余人数-1
        int updateResult = courseMapper.updateCourse(courseId);
        if (updateResult == 1) { //-1成功
            //新增订单
            Order order = new Order();
            order.setCourseId(courseId);
            order.setStuId(stuId);
            int insertResult = orderMapper.insertOrder(order);
            if (insertResult == 1) {  //新增成功
                log.info("下单成功");
                return Result.success(ResultEnum.SUCCESS);
            }
        }
        return Result.failure(ResultEnum.FAILURE);
    }




}
